<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

class Sitemap_model extends CI_Model {
	function __construct() {
		parent::__construct();
	}

	//Get list category by parent_id
	function get_category_arr($cid = '') {
		$cid = intval($cid);
		return $this->db->select('*')->from('tb_olo_category')->where('parent_id', $cid)->get()->result_array();
	}

	//Get list announcement by cat_id
	function get_announcement_arr($cid = '', $pid='', $did='', $limit= '') {
		$cid = intval($cid);
		$did = intval($did);
		$pid = intval($pid);
		if ($cid == 0 && $did==0 && $pid==0) {
			return;
		}

		$sql = "SELECT a.id, a.url_friendly, a.updated, a.created FROM tb_olo_announcement a WHERE display='yes' AND (status='active' OR status='successful' OR status='suspect')";
		if ($cid != 0) {
			$sql .= ' AND a.cid='.$cid;
		}
		if ($pid != 0) {
			$rsProvince = $this->get_distric_arr($pid, true);
			$lsProvince = '(';
			foreach ($rsProvince as $p):
			$lsProvince .= $p['id'];
			if ($p['id'] != $rsProvince[count($rsProvince)-1]['id']) {
				$lsProvince .= ',';
			}
			endforeach;
			$lsProvince .= ')';
			$sql .= ' AND a.did IN ' .$lsProvince;
		}
		if ($did != 0) {
			$sql .= ' AND a.did='.$did;
		}
		$sql .= ' ORDER BY a.created DESC';
		if ($limit != '') {
			$sql .= ' LIMIT '.$limit;

		}
		$arrAnnoun = $this->db->query($sql)->result_array();
		return $arrAnnoun;
	}

	//Get list disctric by province
	function get_distric_arr($pid, $id_arr = false) {
		if ($id_arr) {
			$this->db->select('id');
		} else {
			$this->db->select('*');
		}
		return $this->db->from('tb_districts')->where('pid', $pid)->get()->result_array();
	}

	//Get list project
	function get_project($limit = '') {
		$this->db
		->select('*')
		->from('tb_olo_project')
		->where('active', 'yes');

		if ($limit != '') {
			$this->db->limit(intval($limit));
		}
		$return = $this->db->get()->result_array();
		return $return;
	}

	//Get route from url_friendly
	function get_from_tbl_route($url_friendly){
		return $this->db
		->select('*')
		->from('tb_olo_route')
		->where('url_friendly', $url_friendly)
		->get()
		->row_array();
	}
}
